﻿namespace Nello.Web.Controls
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [ToolboxData("<{0}:ModalDialog runat=server />"), ControlBuilder(typeof(PlaceHolderControlBuilder)), Designer("System.Web.UI.Design.ControlDesigner"), DefaultProperty("Text"), DefaultEvent("Return")]
    public class ModalDialog : TextBox
    {
        private ArrayList list = new ArrayList();

        public event ReturnEventHandler Return;

        public void AddQueryString(string sKey, string sValue)
        {
            sValue = this.Context.Server.UrlEncode(sValue);
            this.list.Add("&" + sKey + "=" + sValue);
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            try
            {
                this.Page.Form.Controls.Add(new LinkButton());
            }
            catch
            {
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            this.AutoPostBack = true;
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            if (this.Return != null)
            {
                this.Return(this.Text);
            }
        }

        public void Open(string url)
        {
            this.Open(url, 600, 400);
        }

        public void Open(string url, int width, int height)
        {
            if (this.list.Count > 0)
            {
                url = url + "?dialogguid='+Math.random()+'";
                IEnumerator ien = this.list.GetEnumerator();
                while (ien.MoveNext())
                {
                    url = url + ien.Current.ToString();
                }
            }
            string arg = string.Format("help:no;status:false;dialogWidth:{0}px;dialogHeight:{1}px", width, height);
            string CS$0$0002 = "<script>var rt=window.showModalDialog('" + url + "',window,'" + arg + "');";
            string js = (CS$0$0002 + "if(rt!=null) document.getElementById('" + this.ClientID + "').value=rt;__doPostBack('" + this.ClientID + "','');") + "</script>";
            this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "ModalDialog" + this.UniqueID, js);
        }

        public void OpenAdd(string url)
        {
            this.AddQueryString("FormType", "Add");
            this.Open(url);
        }

        public void OpenAdd(string url, int width, int height)
        {
            this.AddQueryString("FormType", "Add");
            this.Open(url, width, height);
        }

        public void OpenEdit(string url)
        {
            this.AddQueryString("FormType", "Edit");
            this.Open(url);
        }

        public void OpenEdit(string url, int width, int height)
        {
            this.AddQueryString("FormType", "Edit");
            this.Open(url, width, height);
        }

        protected override void Render(HtmlTextWriter output)
        {
            output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
            output.AddAttribute(HtmlTextWriterAttribute.Name, this.ClientID);
            output.AddAttribute(HtmlTextWriterAttribute.Type, "hidden");
            output.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
            output.RenderBeginTag(HtmlTextWriterTag.Input);
            output.RenderEndTag();
        }

        public static void ReturnBack(Page page, string returnValue)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "ret", "<script language='javascript'>window.returnValue='" + returnValue + "';window.opener=null;window.close();</script>");
        }

        public delegate void ReturnEventHandler(string ReturnText);
    }
}

